<?php

namespace App\Services\Goods;


use App\Models\Goods\Category;
use App\Services\BaseService;

class CatalogService extends BaseService
{
    /**
     * 获取所有1级类目列表
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getL1List()
    {
        return Category::query()
            ->where('level', 'L1')
            ->where('deleted', 0)
            ->get();
    }

    /**
     * 根据一级类目的ID获取二级类目列表
     * @param  int  $pid
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getL2List(int $pid)
    {
        return Category::query()
            ->where('level', 'L2')
            ->where('pid', $pid)
            ->where('deleted', 0)
            ->get();
    }

    /**
     * 根据ID获取一级类目
     * @param  int  $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getL1ById(int $id)
    {
        return Category::query()
            ->where('level', 'L1')
            ->where('id', $id)
            ->where('deleted', 0)
            ->first();
    }
}
